$(function () {

    //左选择
    $('#tlInput').focus(function () {
        $(".showUlleft").slideDown('fast').show();
    });
    $("#tlInput").bind("input propertychange", function (event) {
        var inputval = $('#tlInput').val().toLowerCase();
        $(".showUlleft>li").each(function () {
            var self = $(this);
            if (self.text().toLowerCase().startsWith(inputval)) {
                self.css("display", "block")
            } else {
                self.css("display", "none")
            }
        });
    });

    $(".showUlleft").on("click", "a", function (event) {
        var target = $(event.target);
        $('#tlInput').val($(this).text());//给input赋值
        $(".showUlleft>li>a").each(function () {
            var self = $(this);
            if (self.css("background-color") == "rgb(66, 185, 131)") {
                self.css("background-color", "#ffffff")
            }
        });
        target.css("background-color", "#42B983");
    });
    $('#tlInput').blur(function () {
        $(".showUlleft").slideUp('500').show();
    });


//右选择

    $('#tlLableInput').focus(function () {
        $(".showUlright").slideDown('fast').show();
    });
    $("#tlLableInput").bind("input propertychange", function (event) {
        var inputval = $('#tlLableInput').val().toLowerCase();
        $(".showUlright>li").each(function () {
            var self = $(this);
            if (self.text().toLowerCase().startsWith(inputval)) {
                self.css("display", "block")
            } else {
                self.css("display", "none")
            }
        });
    });

    $(".showUlright").on("click", "a", function (event) {
        var target = $(event.target);
        $('#tlLableInput').val($(this).text());//给input赋值
        $(".showUlright>li>a").each(function () {
            var self = $(this);
            if (self.css("background-color") == "rgb(66, 185, 131)") {
                self.css("background-color", "#ffffff")
            }
        });
        target.css("background-color", "#42B983");
        var a = $(".divlable>p>a");
        if (a.length > 2) {
            ShowFailure("最多三个标签！");
        } else {
            var alist = a.text();
            if (alist.indexOf($(this).text()) == -1) {
                var divlable = $(".divlable");
                $p = $("<p class='pAnsImg'>" + "<a href='javascript:void(0)'>" + $(this).text() + "</a>" + "</p>")
                divlable.append($p);
                if (a.length == 2) {
                    // alert("3")
                    $(".divlable>label").hide();
                    $("#tlLableInput").hide();
                }
            }
        }
        // tlLableInput
    });
    $(".divlable").on("click", "p", function (event) {
        var target = $(event.target);
        console.log(target)
        $(this).remove();
        $("#tlLableInput").show();
        $(".divlable>label").show();
    });

    $('#tlLableInput').blur(function () {
        $(".showUlright").slideUp(500).show();
    });


    $(".file").on("change", "input[type='file']", function () {
        var filePath = $(this).val();

        var arr = filePath.split('\\');
        var fileName = arr[arr.length - 1];
        $(".showFileName").html(fileName);
        // if(filePath.indexOf("jpg")!=-1 || filePath.indexOf("png")!=-1){
        //     $(".fileerrorTip").html("").hide();
        //     var arr=filePath.split('\\');
        //     var fileName=arr[arr.length-1];
        //     $(".showFileName").html(fileName);
        // }else{
        //     $(".showFileName").html("");
        //     $(".fileerrorTip").html("您未上传文件，或者您上传文件类型有误！").show();
        //     return false
        // }
    })

    //number选择
    $('#creditInput').focus(function () {
        $(".showUlnumber").slideDown('fast').show();
    });
    $("#creditInput").bind("input propertychange", function (event) {
        var inputval = $('#tlInput').val().toLowerCase();
        $(".showUlnumber>li").each(function () {
            var self = $(this);
            if (self.text().toLowerCase().startsWith(inputval)) {
                self.css("display", "block")
            } else {
                self.css("display", "none")
            }
        });
    });

    $(".showUlnumber").on("click", "a", function (event) {
        var target = $(event.target);
        $('#creditInput').val($(this).text());//给input赋值
        $(".showUlnumber>li>a").each(function () {
            var self = $(this);
            if (self.css("background-color") == "rgb(66, 185, 131)") {
                self.css("background-color", "#ffffff")
            }
        });
        target.css("background-color", "#42B983");
    });
    $('#creditInput').blur(function () {
        $(".showUlnumber").slideUp('500').show();
    });
    $("#formSubFile").submit(function (e) {

        //校验一,开发语言
        var key = false;
        $("#listsShowUlleft>li>a").each(function () {
            var self = $(this);
            if (self.text() === $("#tlInput").val()) {//发现相等，表示input的值是允许提交到后台的
                key = true;
            }
        });
        if (key != true) {
            ShowFailure("请选择一个开发语言！");
            return false;
        }


        //校验二,标签
        var labelList = "";
        $(".divlable>p>a").each(function () {
            var self = $(this);

            labelList = labelList + self.text() + ";";
        });
        $("#tlLableInput").val(labelList);
        //校验三，title
        if($("#bookTitle").val()==null||$("#bookTitle").val().length<=0){

            ShowFailure("标题不可为空！");
            return false;
        }


        //校验四，简介
        $("#bookDesc").val($("#bookDescTextArea").val());


        // 校验五，积分
        if ($("#creditInput").val() == 0 || $("#creditInput").val() == "0" || $("#creditInput").val() == null || $("#creditInput").val() == "") {
            $("#creditInput").val("0");
        } else if ($("#creditInput").val() != "1") {
            ShowFailure("请选择一个积分数目！");
            return false;
        }

        return true;
    })


})
